<?php
namespace Api\Controller;
use Shop\Api\CategoryApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductGroupApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\SkuApi;
use Shop\Api\SkuvalueApi;
use Admin\Api\UserPictureApi;

/**
 *
 * @author hebiduhebi@126.COM
 */
class ProductController extends ApiController{

    protected  $allowType = array("json","rss","html");


    /**
     * 分页查询
     * pname:名称（模糊查询）(选填)
     * pageNo:当前页
     * pageSize:显示个数
     * cate_id商品分类ID(选填)
     */
    public function query(){
        $notes = "应用".$this->client_id."，调用商品分页查询接口";
        addLog("Product/query",$_GET,$_POST,$notes);

        $pname = $this->_post("pname","");
        $cate_id= $this->_post("cate_id",0);
        $pageNo = $this->_post("pageNo",0);
        $pageSize = $this->_post("pageSize",10);

        $map=array(
            'onshelf'=>1,   //已上架的产品
            'status'=>1,
        );

        if(!empty($pname)){
            $map['name'] = array('like','%'.$pname.'%');
        }

        if($cate_id!=0){
            $map['cate_id']=$cate_id;
        }

        $page = array('curpage'=>$pageNo,'size'=>$pageSize); //分页产品

        $result=apiCall(ProductApi::QUERY_WITH_COUNT,array($map,$page));
        
        //TODO: 需要优化
        for($i=0;$i<count($result['info']['list']);$i++){

           $result['info']['list'][$i]=$this->getImgUrl($result['info']['list'][$i]);

        }


        if($result['status']){
          for($i=0;$i<count($result['info']['list']); $i++){
              $map=array(
                  'id'=>$result['info']['list'][$i]['cate_id'],
              );
              $cate=apiCall(CategoryApi::GET_INFO,array($map));
              $result['info']['list'][$i]['taxrate']=$cate['info']['taxrate'];
          }

           $this->apiReturnSuc($result['info']);
       }else{
           $this->apiReturnErr("暂无商品");
       }
    }




    /**
     * 商品详情展示
     * pid 商品ID
     */
    public function detail(){
        $notes = "应用".$this->client_id."，调用商品详情接口";
        addLog("Product/detail",$_GET,$_POST,$notes);
        $id=$this->_post('id',0,'缺失商品ID');

        $result = apiCall(ProductApi::DETAIL,array($id));

        if(!$result['status']){
            $this->apiReturnErr($result['info']);
        }

        $product = $result['info'];
        if($product['status'] == -1){
            $this->apiReturnErr("该商品已被删除!");
        }

        unset($product['createtime']);
        unset($product['updatetime']);

        unset($product['onshelf']);
        unset($product['status']);
        unset($product['pid']);
        unset($product['uid']);

        $product = $this->getImgUrl($product);

        $this->apiReturnSuc($product);

    }

    private function getImgUrl($result){

        $img_url_arr = explode(',',rtrim($result['img'],","));
        $img_url="";

        foreach($img_url_arr as $im){
            $img_url .= getImageUrl($im,400).",";
        }

        $result['img']=rtrim($img_url,",");

        $result['main_img']= getImageUrl($result['main_img'],400);
        $result['img_post']= getImageUrl($result['img_post']);
        $result['img_post_bg']=getImageUrl($result['img_post_bg']);

        $result['detail'] = htmlspecialchars_decode($result['detail']);
        return $result;
    }


    /**
     * 分页查询
     * pageNo:当前页
     * pageSize:显示个数
     * cate_id商品分类ID(选填)
     */
    public function appIndex(){
        $notes = "应用".$this->client_id."，调用商品分页查询接口";
        addLog("Product/query",$_GET,$_POST,$notes);
        $map=array(
            'onshelf'=>1,   //已上架的产品
            'status'=>1,
        );
        $cate_id=I("cate_id",0);
        if($cate_id!=0){
            $map['cate_id']=$cate_id;
        }
        $page = array('curpage'=>I('pageNo',0),'size'=>I('pageSize',10)); //分页产品

        $result=apiCall(ProductApi::QUERY_WITH_COUNT,array($map,$page));

        for($i=0;$i<count($result['info']['list']);$i++){

            $result['info']['list'][$i]=$this->getImgUrl($result['info']['list'][$i]);
        }

        if($result['status']){
            for($i=0;$i<count($result['info']['list']); $i++){
                $map=array(
                    'id'=>$result['info']['list'][$i]['cate_id'],
                );
                $cate=apiCall(CategoryApi::GET_INFO,array($map));
                $result['info']['list'][$i]['taxrate']=$cate['info']['taxrate'];
            }

            $this->apiReturnSuc($result['info']);
        }else{
            $this->apiReturnErr("暂无商品");
        }
    }


    function getSupportMethod()
    {

    }
}